SUBROUTINE readinp
USE param
USE global
IMPLICIT NONE
INTEGER :: i
INTEGER :: n_line,asci
! Initial values
1100 FORMAT(3X,'DATE ',I2.2, '/', I2.2, '/', I4.4, '; time ',&
           I2.2, ':', I2.2, ':', I2.2 )
1101 FORMAT('!--------------------------------------------------!')
1102 FORMAT(3X,'Mtok-Ver0.20')
!---------------------------------------------------------------!
! Message for starting
sys_etime=ETIME(sys_time)
CALL IDATE(today)  ! today(1)=day, (2)=month, (3)=year
CALL ITIME(now)    ! now(1)=hour, (2)=minute (3)=second

!---------------------------------------------------------------!
! Initializing variables
n_lammps_file=0
pkae=0.0
!---------------------------------------------------------------!
! Read input file
CALL GETARG(1,ipfn)
OPEN(UNIT=1,FILE=ipfn,STATUS='OLD',ERR=998)
DO
   eatline=''
   READ(1,'(A)',END=100,ERR=999) eatline
   n_line=n_line+1
   IF(LEN_TRIM(eatline).EQ.0) CYCLE
   eatline=ADJUSTL(eatline)
   asci=ICHAR(eatline(1:1))
   IF(asci.EQ.35) CYCLE
   READ(eatline,*,END=101) (eatword(i),i=1,mn_word)
101 CONTINUE
   SELECT CASE(eatword(1))
   CASE("case")
      comment_line=eatline
   CASE("pkae")
      READ(eatword(2),*) pkae
   CASE("lat_structure")
      READ(eatword(2),*) ltype
   CASE("lat_distance")
      READ(eatword(2),*) dist_a0_nm
      ! nm->A
      unit_conv=10.0
      dist_a0_ang=dist_a0_nm*unit_conv
   CASE("remove_layer")
      READ(eatword(2),*) n_rlayer
   case("add_layer")
      read(eatword(2),*) n_alayer
   CASE("lammps_out")
      READ(eatword(2),*) n_lammps_file
      ! Read from output file of lammps
      ALLOCATE(lammps_file(n_lammps_file))
      DO i=1,n_lammps_file
         READ(1,*) lammps_file(i)%source
         lammps_file(i)%outfile=TRIM(ADJUSTL(lammps_file(i)%source))//'.c'
      END DO
   CASE("xmakemol")
      READ(eatword(2),*) xmfn
      ! Create xmakemol file
      OPEN(UNIT=2,FILE=xmfn,STATUS='REPLACE')
      CLOSE(2)
   CASE("log")
      READ(eatword(2),*) lgfn
   CASE DEFAULT
   END SELECT
END DO
998 CONTINUE
STOP 'No such file exists. Check the name of the input file.'
999 CONTINUE
STOP 'Error occur during reading input file (readinp)'
100 CONTINUE
CLOSE(1)
! Checking for null input
IF(n_lammps_file.EQ.0) STOP 'ERR:Specify input file name of LAMMPS.'
IF(pkae.EQ.0) STOP 'ERR:Specify energy of PKA(pkae).'
! Write starting message to logfile and screen
OPEN(UNIT=1,FILE=lgfn,STATUS='REPLACE')
WRITE(1,*)
PRINT *
WRITE(1,1101)
PRINT 1101
WRITE(1,1102)
PRINT 1102
WRITE(1,1100) today(2),today(1),today(3),now
PRINT 1100,today(2),today(1),today(3),now
WRITE(1,1101)
PRINT 1101
WRITE(1,*)
PRINT *
CLOSE(1)
! Input checking....
1000 FORMAT(3X,'>>> ',A20)
1001 FORMAT(3X,'Number of source files :',I6)
OPEN(UNIT=1,FILE=lgfn,STATUS='OLD',ACCESS='APPEND')
WRITE(1,1000) ADJUSTL(comment_line)
PRINT 1000,ADJUSTL(comment_line)
WRITE(1,1001) n_lammps_file
PRINT 1001,n_lammps_file
CLOSE(1)
END SUBROUTINE readinp
